//
//  ViewController.m
//  菜单的选择
//
//  Created by 李lucy on 16/4/18.
//  Copyright © 2016年 com.liluxin.test.Co.,Ltd. All rights reserved.
//

#import "ViewController.h"
#import "LX_categories.h"
#import "LX_TopWindowViewController.h"

#define width [UIScreen mainScreen].bounds.size.width * 0.5
#define height [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

/** catagarysTableview */
@property (nonatomic, strong) UITableView *catagaryTableView;
/** subCatarysTableview */
@property (nonatomic, strong) UITableView *subCatagaryTableView;

/** 分类数组 */
@property (nonatomic, strong) NSArray *catagarys;
@end

@implementation ViewController


- (NSArray *)catagarys
{
    if (!_catagarys) {
    
        
        NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"categories" ofType:@"plist" ]];
        NSMutableArray *arrM = [NSMutableArray array];
        for (NSDictionary *dict in arr) {
            [arrM addObject:[LX_categories categoryWithDictionary:dict]];
        }
        _catagarys = arrM;
    }
    return _catagarys;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//   STATUSBAR_LIGHT
//   STATUSBAR_DEFAULT
    
    [LX_TopWindowViewController sharedInstance].statusBarStyle = UIStatusBarStyleLightContent;
    
    
    [self.view addSubview:self.catagaryTableView];
    [self.view addSubview:self.subCatagaryTableView];
    
    self.subCatagaryTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    [self.catagaryTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
    
}



- (UITableView *)catagaryTableView
{
    if (!_catagaryTableView) {
        _catagaryTableView = [[UITableView alloc] init];
        _catagaryTableView.frame = CGRectMake(0, 0, width, height);
        _catagaryTableView.delegate = self;
        _catagaryTableView.dataSource = self;

    }
    return _catagaryTableView;
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    NSLog(@"catagary-----%@",NSStringFromUIEdgeInsets(self.catagaryTableView.contentInset));
        NSLog(@"subcatagary-----%@",NSStringFromUIEdgeInsets(self.subCatagaryTableView.contentInset));
}
- (UITableView *)subCatagaryTableView
{
    if (!_subCatagaryTableView) {
        _subCatagaryTableView = [[UITableView alloc] init];
        _subCatagaryTableView.frame = CGRectMake(width, 0, width, height);
        _subCatagaryTableView.delegate =  self;
        _subCatagaryTableView.dataSource = self;
    }
    return _subCatagaryTableView;
}


static NSString *const catagaryCellID = @"catagaryCellID";
static NSString *const subCatagaryCellID = @"subCatagaryCellID";
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.catagaryTableView) {//一级
        return self.catagarys.count;
    }else{
        LX_categories *c = self.catagarys[self.catagaryTableView.indexPathForSelectedRow.row];
        NSLog(@"%zd",c.subcategories.count);
        return c.subcategories.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.catagaryTableView) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:catagaryCellID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:catagaryCellID];
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        LX_categories *catagary = self.catagarys[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:catagary.icon];
        cell.imageView.highlightedImage = [UIImage imageNamed:catagary.highlighted_icon];
        
        cell.textLabel.text = catagary.name;
        cell.textLabel.highlightedTextColor = [UIColor redColor];
     
        cell.backgroundColor = [UIColor grayColor];
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:subCatagaryCellID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:subCatagaryCellID];
        }
        LX_categories *c = self.catagarys[self.catagaryTableView.indexPathForSelectedRow.row];
        
        cell.textLabel.text = c.subcategories[indexPath.row];
        cell.textLabel.highlightedTextColor = [UIColor blueColor];
//        cell.textLabel.text = [NSString stringWithFormat:@"二级测试数据%ld",(long)indexPath.row];
        cell.backgroundColor = [UIColor greenColor];
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.catagaryTableView) {
        [self.subCatagaryTableView reloadData];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
